#include "tracklistform.h"
#include "ui_tracklistform.h"

TrackListForm::TrackListForm(Geocaching *aProg, QWidget *parent) :
    QWidget(parent), ui(new Ui::TrackListForm), myProg(aProg)
{
    ui->setupUi(this);
    setFixedSize(size());
    connect(myProg, SIGNAL(TrackAdded(Track)), this, SLOT(UpdateForm()));
    connect(myProg, SIGNAL(TrackDeleted(ulong)), this, SLOT(UpdateForm()));
    connect(myProg, SIGNAL(CurrentTrackChanged()), this, SLOT(UpdateForm()));
}

TrackListForm::~TrackListForm()
{
    delete ui;
}

void TrackListForm::activateWindow()
{
    UpdateForm();
}

void TrackListForm::UpdateForm(void)
{
    ui->trackListWidget->clear();
    ui->propertyListWidget->clear();
    QList <Track> formTrackList = myProg->GetTrackList();
    foreach(Track tmpTrack, formTrackList)
    {
        QListWidgetItem *item = new QListWidgetItem(tmpTrack.GetName(), ui->trackListWidget);
        item->setData(Qt::UserRole, static_cast<qulonglong>(tmpTrack.GetID()));
        item->setText(tmpTrack.GetName());
        if(tmpTrack.GetID() == myProg->GetCurrentTrackID()) item->setTextColor(Qt::darkRed);
        else                                                item->setTextColor(Qt::black);
    }
    QListWidgetItem *item = ui->trackListWidget->currentItem();
    if(!item)
    {
        ui->deleteTrackButton->setEnabled(false);
        ui->viewSelectedButton->setEnabled(false);
    }
    else
    {
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        ui->deleteTrackButton->setEnabled(tmpID);
        ui->viewSelectedButton->setEnabled(tmpID != myProg->GetCurrentTrackID());
    }
}

void TrackListForm::on_deleteTrackButton_clicked()
{
    QListWidgetItem *item = ui->trackListWidget->currentItem();
    if(item)
    {
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        myProg->DeleteTrack(tmpID);
    }
}

void TrackListForm::on_trackListWidget_doubleClicked(const QModelIndex& /*index*/)
{
    ui->propertyListWidget->clear();
    QListWidgetItem *item = ui->trackListWidget->currentItem();
    if(item)
    {
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        Track tmpTrack;
        if(myProg->GetTrack(tmpID, &tmpTrack))
        {
            QString tmpString;
            unsigned long N = tmpTrack.GetPointCount();
            QListWidgetItem *itemProperty = new QListWidgetItem(tmpTrack.GetName(), ui->propertyListWidget);
            if(N > 0)   tmpString.sprintf("Список точек трека.");
            else        tmpString.sprintf("В треке нет точек.");
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            TrackPoint tmpPoint;
            for(unsigned long i = 0; i < N; ++i)
            {
                tmpTrack.GetPoint(&tmpPoint, i);
                tmpString.sprintf("Дата и время: ");
                tmpString += tmpPoint.GetDateTime().toString();
                itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
                tmpString.sprintf("Широта: %lf", tmpPoint.GetCoordinates().GetLatitude());
                itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
                tmpString.sprintf("Долгота: %lf", tmpPoint.GetCoordinates().GetLongitude());
                itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            }
        }
    }
}

void TrackListForm::on_trackFindLineEdit_returnPressed()
{
    if(!ui->trackFindLineEdit->text().isEmpty())
    {
        unsigned long findStartNumber = ui->trackListWidget->currentRow(), N = ui->trackListWidget->count();
        for(unsigned long i = ++findStartNumber; i < N; ++i)
        {
            QString tmpString = ui->trackListWidget->item(i)->text();
            tmpString.resize(ui->trackFindLineEdit->text().size());
            if(!tmpString.compare(ui->trackFindLineEdit->text()))
            {
                ui->trackListWidget->setCurrentRow(i);
                return;
            }
        }
        for(unsigned long i = 0; i < findStartNumber; ++i)
        {
            QString tmpString = ui->trackListWidget->item(i)->text();
            tmpString.resize(ui->trackFindLineEdit->text().size());
            if(!tmpString.compare(ui->trackFindLineEdit->text()))
            {
                ui->trackListWidget->setCurrentRow(i);
                return;
            }
        }
    }
}

void TrackListForm::on_trackListWidget_currentRowChanged(int currentRow)
{
    ui->propertyListWidget->clear();
    if(currentRow == -1)
    {
        ui->deleteTrackButton->setEnabled(false);
        ui->viewSelectedButton->setEnabled(false);
        ui->routeButton->setEnabled(false);
    }
    else
    {
        QListWidgetItem *item = ui->trackListWidget->currentItem();
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        ui->deleteTrackButton->setEnabled(tmpID);
        ui->viewSelectedButton->setEnabled(tmpID != myProg->GetCurrentTrackID());
        ui->routeButton->setEnabled(tmpID);
    }
}

void TrackListForm::on_viewSelectedButton_clicked()
{
    QListWidgetItem *item = ui->trackListWidget->currentItem();
    if(item)
    {
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        myProg->SetCurrentTrack(tmpID);
        ui->hideSelectedButton->setEnabled(true);
    }
}

void TrackListForm::on_hideSelectedButton_clicked()
{
    myProg->SetCurrentTrack(0);
    ui->hideSelectedButton->setEnabled(false);
}

void TrackListForm::on_routeButton_clicked()
{
    QListWidgetItem *item = ui->trackListWidget->currentItem();
    TrackToRouteDialog trDialog(myProg, item->data(Qt::UserRole).toULongLong());
    trDialog.setWindowFlags(trDialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
    trDialog.exec();
}
